#include "GamePacket.h"
#include <QDataStream>
#include <QDebug>

// PlayerInfo 实现
PlayerInfo::PlayerInfo() : playerId(0), port(0) 
{
}

PlayerInfo::PlayerInfo(quint32 id, const QString& name, const QHostAddress& addr, quint16 p)
    : playerId(id), playerName(name), address(addr), port(p) 
{
}

// GamePacket 实现
GamePacket::GamePacket() : type(PacketType::PLAYER_JOIN), playerId(0), timestamp(0) 
{
}

GamePacket::GamePacket(PacketType t, quint32 id) : type(t), playerId(id), timestamp(QDateTime::currentMSecsSinceEpoch()) 
{
}

QByteArray GamePacket::serialize() const
{
    QByteArray data;
    QDataStream stream(&data, QIODevice::WriteOnly);
    
    stream << static_cast<quint16>(type) << playerId << timestamp;
    
    return data;
}

std::unique_ptr<GamePacket> GamePacket::deserialize(const QByteArray& data)
{
    if (data.isEmpty()) 
    {
        return nullptr;
    }
    
    QDataStream stream(data);
    
    quint16 type;
    quint32 playerId;
    quint32 timestamp;
    
    stream >> type >> playerId >> timestamp;
    
    PacketType packetType = static_cast<PacketType>(type);
    
    std::unique_ptr<GamePacket> packet;
    
    switch (packetType) 
    {
        case PacketType::PLAYER_JOIN:
        case PacketType::PLAYER_LEAVE:
        case PacketType::GAME_START:
        {
            packet = std::make_unique<GamePacket>(packetType, playerId);
            break;
        }
        case PacketType::GAME_STATUS: 
        {
            auto statusPacket = std::make_unique<GameStatusPacket>();
            statusPacket->type = packetType;
            statusPacket->playerId = playerId;
            statusPacket->timestamp = timestamp;
            
            // 反序列化玩家数据（包含所有战斗属性）
            quint8 gameState;
            stream >> statusPacket->playerData.level >> statusPacket->playerData.health 
                   >> statusPacket->playerData.maxHealth >> statusPacket->playerData.attackPower 
                   >> statusPacket->playerData.defense >> statusPacket->playerData.critChance
                   >> statusPacket->playerData.critMultiplier >> statusPacket->playerData.hitChance
                   >> statusPacket->playerData.dodgeChance >> statusPacket->playerData.totalKills 
                   >> gameState >> statusPacket->playerData.isInAutoCombat;
            statusPacket->playerData.gameState = static_cast<GameState>(gameState);
            
            // 反序列化怪物数据（包含所有战斗属性）
            stream >> statusPacket->monsterData.level >> statusPacket->monsterData.health 
                   >> statusPacket->monsterData.maxHealth >> statusPacket->monsterData.attackPower 
                   >> statusPacket->monsterData.defense >> statusPacket->monsterData.critChance
                   >> statusPacket->monsterData.critMultiplier >> statusPacket->monsterData.hitChance
                   >> statusPacket->monsterData.dodgeChance >> statusPacket->monsterData.name;
            
            stream >> statusPacket->message;
            
            packet = std::move(statusPacket);
            break;
        }
        case PacketType::ATTACK: 
        {
            auto attackPacket = std::make_unique<AttackPacket>();
            attackPacket->type = packetType;
            attackPacket->playerId = playerId;
            attackPacket->timestamp = timestamp;
            
            stream >> attackPacket->damage >> attackPacket->targetHealth >> attackPacket->message;
            
            packet = std::move(attackPacket);
            break;
        }
        case PacketType::LEVEL_UP: 
        {
            auto levelUpPacket = std::make_unique<LevelUpPacket>();
            levelUpPacket->playerId = playerId;
            levelUpPacket->timestamp = timestamp;
            
            // 反序列化玩家数据
            quint8 gameState;
            stream >> levelUpPacket->newPlayerData.level >> levelUpPacket->newPlayerData.health 
                   >> levelUpPacket->newPlayerData.maxHealth >> levelUpPacket->newPlayerData.attackPower 
                   >> levelUpPacket->newPlayerData.totalKills >> gameState
                   >> levelUpPacket->newPlayerData.isInAutoCombat;  // 新增字段
            levelUpPacket->newPlayerData.gameState = static_cast<GameState>(gameState);
            
            stream >> levelUpPacket->message;
            
            packet = std::move(levelUpPacket);
            break;
        }
        case PacketType::COMBAT_MESSAGE:  // 新增消息包类型
        {
            auto combatPacket = std::make_unique<CombatMessagePacket>();
            combatPacket->type = packetType;
            combatPacket->playerId = playerId;
            combatPacket->timestamp = timestamp;
            
            stream >> combatPacket->message;
            
            packet = std::move(combatPacket);
            break;
        }
        default:
            return nullptr;
    }
    
    return packet;
}

// GameStatusPacket 实现
GameStatusPacket::GameStatusPacket() : GamePacket(PacketType::GAME_STATUS, 0) 
{
}

GameStatusPacket::GameStatusPacket(quint32 id, const PlayerGameData& pData, const MonsterData& mData, const QString& msg) 
    : GamePacket(PacketType::GAME_STATUS, id), playerData(pData), monsterData(mData), message(msg) 
{
}

QByteArray GameStatusPacket::serialize() const
{
    QByteArray data = GamePacket::serialize();
    QDataStream stream(&data, QIODevice::WriteOnly | QIODevice::Append);
    
    // 序列化玩家数据（包含所有战斗属性）
    stream << playerData.level << playerData.health << playerData.maxHealth 
           << playerData.attackPower << playerData.defense << playerData.critChance
           << playerData.critMultiplier << playerData.hitChance << playerData.dodgeChance
           << playerData.totalKills << static_cast<quint8>(playerData.gameState)
           << playerData.isInAutoCombat;
    
    // 序列化怪物数据（包含所有战斗属性）
    stream << monsterData.level << monsterData.health << monsterData.maxHealth 
           << monsterData.attackPower << monsterData.defense << monsterData.critChance
           << monsterData.critMultiplier << monsterData.hitChance << monsterData.dodgeChance
           << monsterData.name;
    
    // 序列化消息
    stream << message;
    
    return data;
}

// AttackPacket 实现
AttackPacket::AttackPacket() : GamePacket(PacketType::ATTACK, 0), damage(0), targetHealth(0) 
{
}

AttackPacket::AttackPacket(PacketType type, quint32 id, quint32 dmg, quint32 health, const QString& msg) 
    : GamePacket(type, id), damage(dmg), targetHealth(health), message(msg) 
{
}

QByteArray AttackPacket::serialize() const
{
    QByteArray data = GamePacket::serialize();
    QDataStream stream(&data, QIODevice::WriteOnly | QIODevice::Append);
    
    stream << damage << targetHealth << message;
    
    return data;
}

// LevelUpPacket 实现
LevelUpPacket::LevelUpPacket() : GamePacket(PacketType::LEVEL_UP, 0) 
{
}

LevelUpPacket::LevelUpPacket(quint32 id, const PlayerGameData& data, const QString& msg) 
    : GamePacket(PacketType::LEVEL_UP, id), newPlayerData(data), message(msg) 
{
}

QByteArray LevelUpPacket::serialize() const
{
    QByteArray data = GamePacket::serialize();
    QDataStream stream(&data, QIODevice::WriteOnly | QIODevice::Append);
    
    // 序列化新玩家数据
    stream << newPlayerData.level << newPlayerData.health << newPlayerData.maxHealth 
           << newPlayerData.attackPower << newPlayerData.totalKills 
           << static_cast<quint8>(newPlayerData.gameState)
           << newPlayerData.isInAutoCombat;  // 新增字段
    
    stream << message;
    
    return data;
}

// CombatMessagePacket 实现
CombatMessagePacket::CombatMessagePacket() : GamePacket(PacketType::COMBAT_MESSAGE, 0) 
{
}

CombatMessagePacket::CombatMessagePacket(quint32 id, const QString& msg) 
    : GamePacket(PacketType::COMBAT_MESSAGE, id), message(msg) 
{
}

QByteArray CombatMessagePacket::serialize() const
{
    QByteArray data = GamePacket::serialize();
    QDataStream stream(&data, QIODevice::WriteOnly | QIODevice::Append);
    
    stream << message;
    
    return data;
} 